package com.chinaunicom.product.app.config;

import com.chinaunicom.common.constants.Constants;
import com.chinaunicom.product.trigger.listener.ProductStockListener;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.spring.autoconfigure.RocketMQProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author guomenghuai
 * @date 2024/1/10
 * @Description
 */
@Configuration
public class ConsumerConfig {

    @Autowired
    private RocketMQProperties rocketMQProperties;

    @Bean("productStockConsumer")
    public DefaultMQPushConsumer productStockConsumer(ProductStockListener listener) throws MQClientException {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(Constants.RocketMqConstants.SKYWALKING_CLOUD_DEFAULT_PRODUCER_GROUP);
        consumer.setNamesrvAddr(rocketMQProperties.getNameServer());
        consumer.subscribe(Constants.RocketMqConstants.SKYWALKING_CLOUD_DEFAULT_PRODUCER_GROUP, "*");
        consumer.registerMessageListener(listener);
        consumer.start();
        return consumer;
    }
}
